package cn.edu.tju.wiki.normalization;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import cn.edu.tju.wiki.utils.DataCon;

public class DocumentsToDatabase {

	private static final String filePath = "/Users/liudajian/Desktop/paradise/古代文献/";
	private static final String fileName = "竹书纪年.txt";//"尚书.txt";//"春秋谷梁传.txt";//"礼记.txt";//"国语.txt";//
	private static final String scheme = "document_data";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			String encoding = "UTF8";
			File file = new File(filePath+fileName);
			
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				StringBuffer contentStr = new StringBuffer();
				String lineTxt = null;
				String docBook = fileName.split("\\.")[0];
				String subDocBook = "";
				while ((lineTxt = bufferedReader.readLine()) != null) {
					if(lineTxt.length() > 0){
						System.out.println(lineTxt);
						if("春秋谷梁传.txt".equals(fileName)){
							if(lineTxt.contains("——————")){
								if(contentStr.toString().length() > 0){
									saveContent(docBook+"-"+subDocBook, contentStr.toString());
									contentStr = new StringBuffer();
								}
								subDocBook = lineTxt.split("——————")[1];
							}else{
								contentStr.append(lineTxt.replaceAll(" ", ""));
							}
						}else if("国语.txt".equals(fileName)){
							if(lineTxt.contains("                               ")){
								if(contentStr.toString().length() > 0){
									saveContent(docBook+"-"+subDocBook, contentStr.toString());
									contentStr = new StringBuffer();
								}
								subDocBook = lineTxt.split("                               ")[1];
							}else{
								contentStr.append(lineTxt.replaceAll(" ", ""));
							}
						}else if("礼记.txt".equals(fileName)){
							if(lineTxt.contains("——————")){
								if(contentStr.toString().length() > 0){
									saveContent(docBook+"-"+subDocBook, contentStr.toString());
									contentStr = new StringBuffer();
								}
								subDocBook = lineTxt.split("——————")[1];
							}else{
								contentStr.append(lineTxt.replaceAll(" ", ""));
							}
						}else if("尚书.txt".equals(fileName)){
							if(lineTxt.contains("——————")){
								if(contentStr.toString().length() > 0){
									saveContent(docBook+"-"+subDocBook, contentStr.toString());
									contentStr = new StringBuffer();
								}
								subDocBook = lineTxt.split("——————")[1];
							}else{
								contentStr.append(lineTxt.replaceAll(" ", ""));
							}
						}
						else if("竹书纪年.txt".equals(fileName)){
							if(lineTxt.contains("——————")){
								if(contentStr.toString().length() > 0){
									saveContent(docBook+"-"+subDocBook, contentStr.toString());
									contentStr = new StringBuffer();
								}
								subDocBook = lineTxt.split("——————")[1];
							}else{
								contentStr.append(lineTxt.replaceAll(" ", ""));
							}
						}
					}
				}
				saveContent(docBook+"-"+subDocBook, contentStr.toString());
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
	}
	
	public static void saveContent(String doc_book, String doc_document) throws ClassNotFoundException{
		System.out.println(doc_book + "#####"+doc_document);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("doc_book", doc_book);
		dataMap.put("doc_content", doc_document);
//		DataCon.insertSQL(scheme, dataMap);
	}

}
